package io.tim.timongodb;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.tim.timongodb.util.ClassUtil;
import io.tim.timongodb.util.InstanceXMLBuilder;

/**
 * 
 * 负责数据库实例的建库、建表等操作
 *
 * @author zhangnan
 * @date 2017年4月24日
 *
 */
public class InstanceHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtil.class);

	private DBInstance dbInstance = null;

	public InstanceHandler() {

	}

	public InstanceHandler(DBInstance dbInstance) {
		if (dbInstance != null) {
			this.dbInstance = dbInstance;
			init();
		}

	}

	private void init() {

	}

	public DBInstance getDbInstance() {
		return dbInstance;
	}

	public void setDbInstance(DBInstance dbInstance) {
		this.dbInstance = dbInstance;
	}

	// 列出实例下的所有数据库
	public void lisInstanceDatabases(String instancePath, String instanceFileName) {
		InstanceXMLBuilder.lisXMLNodes(instancePath + instanceFileName);
	}

	// 插入数据库信息
	public void addDatabaseToInstance(String dbName, String dbFile) {
		this.dbInstance.addDatabase(dbName, new Database(this.dbInstance.getInstanceName(), dbName));
		InstanceXMLBuilder.AddDatabaseToInstanceXML(this.dbInstance.getInstancePath(),
				this.dbInstance.getInstanceFileName(), dbName, dbFile);
	}

	// 创建数据库
	public void createDataBase(String dbName, String dbFileName) {

		this.addDatabaseToInstance(dbName, dbFileName.substring(dbFileName.lastIndexOf("/") + 1));
		InstanceXMLBuilder.NewDataBaseXML(this.dbInstance.getInstanceName(), dbName, dbFileName);
	}

	// 判断是否是数据库，如果是则返回数据库的路径，否则返回notFound
	public String isDatabase(String dbName) {
		String dbFileName = null;
		if (this.dbInstance.getDatabases().containsKey(dbName)) {
			dbFileName = InstanceXMLBuilder
					.getDBFileName(this.dbInstance.getInstancePath() + this.dbInstance.getInstanceFileName(), dbName);
		}
		return dbFileName;
	}

	// 建表
	public void createTable(String dbName, String tableName) {
		// 根据数据库名称获取是否存在该数据库，如果存在则返回数据库文件名称，否则返回 null
		String dbFileName = this.isDatabase(dbName);
		if (dbFileName == null) {
			System.out.println("没有找到数据库 " + dbName +"！");
		} else {
			InstanceXMLBuilder.addTableToDBXML("src/main/resources/instances/databases/" + dbFileName, tableName);
		}
	}

	public static void main(String[] args) {
		Map<String, InstanceHandler> dbInstances = DBMS.startDBMS().withDBInstance("Inst01");
		InstanceHandler dbInst01Handler = dbInstances.get("Inst01");
		DBInstance dbInst02 = new DBInstance("Inst02");
		Map<String, String> dbMap = new HashMap<String, String>();
		dbMap.put("db01", "db01.xml");
		dbMap.put("db02", "db02.xml");
		dbMap.put("db03", "db03.xml");
		dbMap.put("db04", "db04.xml");
		InstanceHandler dbInst02Handler = new InstanceHandler(dbInst02);

		Iterator<Entry<String, String>> iter = dbMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, String> entry = iter.next();
			String dbName = entry.getKey();
			String dbFile = entry.getValue();

			String dbFileName = "src/main/resources/instances/databases/"
					+ dbInst02Handler.getDbInstance().getInstanceName() + "." + dbFile;
			dbInst02Handler.createDataBase(dbName, dbFileName);

		}
		String dbName = "db01";
		String dbFile = "db01.xml";
		String dbFileName = "src/main/resources/instances/databases/"
				+ dbInst02Handler.getDbInstance().getInstanceName() + "." + dbFile;
		dbInst02Handler.createDataBase(dbName, dbFileName);

		try {
			dbInst02Handler.lisInstanceDatabases(dbInst02.getInstancePath(), dbInst02.getInstanceFileName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		dbInst02Handler.createTable("db01", "table01");
	}

}
